/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rejeev.tracer;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.AdviceAdapter;

/**
 *
 * @author rejeev.divakaran
 */
public class TracerMethodAdapter extends AdviceAdapter {
    private String mName;
    //private MethodVisitor mv;
    private String cName;
    public TracerMethodAdapter(String cName, int access, String name, String desc, MethodVisitor mv){
        super(mv, access, name, desc);
        this.mName = name;
        this.mv = mv;
        this.cName = cName;
    }

    @Override
    protected void onMethodEnter() {
        try{
            mv.visitLdcInsn(cName);
            mv.visitLdcInsn(mName);
            mv.visitMethodInsn(INVOKESTATIC, "rejeev/tracer/Tracer", "entry", "(Ljava/lang/String;Ljava/lang/String;)V");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onMethodExit(int arg0) {
        try{
            mv.visitLdcInsn(cName);
            mv.visitLdcInsn(mName);
            mv.visitMethodInsn(INVOKESTATIC, "rejeev/tracer/Tracer", "exit", "(Ljava/lang/String;Ljava/lang/String;)V");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    @Override
    public void visitMaxs(int maxStack, int maxLocals){
        super.visitMaxs(maxStack + 4, maxLocals);
    }
}
